package com.device.grant.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.device.grant.dao.ConsumeinfoDao;
import com.device.grant.dao.DepartmentDao;
import com.device.grant.pojo.Consumeinfo;
import com.device.grant.pojo.IDepartment;
import com.device.grant.pojo.IDevice;
import com.device.grant.pojo.IEmployee;
import com.device.grant.pojo.vo.ConsumeinfoVo;
import com.device.grant.service.ConsumeinfoService;
import com.device.grant.service.IDeviceService;
import com.device.grant.service.IemployeeService;
import com.device.grant.utils.BeanHelper;
import com.device.grant.utils.ColumnCompator;

@Service("consumeinfoService")
public class ConsumeinfoServiceImpl implements ConsumeinfoService {

	@Autowired
	private ConsumeinfoDao		consumeinfoDao;

	@Resource(name = "departmentDao")
	private DepartmentDao		departmentDao;

	@Autowired
	private IDeviceService		iDeviceService;

	@Autowired
	private IemployeeService	iemployeeService;

	public Integer countConsumeinfoInPageForTime(Map<String, Object> conditions, List<String> blearParams,
			Map<String, ColumnCompator> compators) {
		return consumeinfoDao.countConsumeinfoInPageForTime(conditions, blearParams, compators);
	}

	public List<ConsumeinfoVo> queryAppointmentsInPageForTime(Integer page, Integer rows, Map<String, Object> conditions,
			List<String> blearParams, Map<String, ColumnCompator> compators) {
		List<Consumeinfo> infos = consumeinfoDao.queryAppointmentsInPageForTime(page, rows, conditions, blearParams, compators);

		List<ConsumeinfoVo> vos = new ArrayList<ConsumeinfoVo>();

		Set<Integer> deptSet = new HashSet<Integer>();
		Set<Integer> deviceSet = new HashSet<Integer>();
		Set<String> empNos = new HashSet<String>();

		for (Consumeinfo info : infos) {
			vos.add(BeanHelper.getInstanceFromOrigBean(ConsumeinfoVo.class, info));
			deptSet.add(info.getDeptID());
			deviceSet.add(info.getDeviceID());
			empNos.add(info.getEmpno());
		}

		if (!deptSet.isEmpty()) {
			List<IDepartment> dpts = departmentDao.getDepartMentsIncludeParent(deptSet);

			for (ConsumeinfoVo vo : vos) {
				for (IDepartment department : dpts) {
					if (vo.getDeptID().equals(department.getDeptID())) {
						vo.setDeptName(department.getDeptName());
					}
				}
			}
		}

		if (!deviceSet.isEmpty()) {
			List<IDevice> ids = iDeviceService.queryIDeviceByIds(deviceSet);

			for (ConsumeinfoVo vo : vos) {
				for (IDevice device : ids) {
					if (vo.getDeviceID().equals(device.getDeviceID())) {
						vo.setDevName(device.getDevName());
					}
				}
			}
		}

		if (!empNos.isEmpty()) {
			List<IEmployee> employees = iemployeeService.queryEmployeeSInEmpNos(empNos);

			for (ConsumeinfoVo vo : vos) {
				for (IEmployee ie : employees) {
					if (StringUtils.trim(vo.getEmpno()).equals(StringUtils.trim(ie.getEmpNO()))) {
						vo.setEmpName(ie.getEmpName());
					}
				}
			}
		}

		return vos;
	}

}
